package com.ag.lzw.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.ag.bean.TbEvent;
import com.ag.bean.TbFeedbackAnnex;
import com.ag.bean.TbOverRequest;
import com.ag.bean.TbTask;
import com.ag.bean.TbTaskAnnex;
import com.ag.bean.TbTaskFeedback;
import com.ag.bean.TbTaskStatus;
import com.ag.bean.TbUser;
import com.ag.bean.TbUserDept;
import com.ag.common.AnnexPath;
import com.ag.common.PageUtil;
import com.ag.dao.TbTaskStatusDAO;
import com.ag.lwc.service.impl.LogService;
import com.ag.lzw.common.DateHandeler;
import com.ag.lzw.common.TaskTreeManager;
import com.ag.lzw.common.TbOverStatus;
import com.ag.lzw.common.TbOverStatusDAO;
import com.ag.lzw.dao.LzwTaskDAO;
import com.ag.lzw.dao.LzwTbEventDAO;
import com.ag.lzw.dao.LzwTbFeedbackAnnexDAO;
import com.ag.lzw.dao.LzwTbOverRequestDAO;
import com.ag.lzw.dao.LzwTbTaskAnnexDAO;
import com.ag.lzw.dao.LzwTbTaskFeedbackDAO;
import com.ag.lzw.dao.LzwTbUserDAO;
import com.ag.lzw.service.HandelTasks;
import com.ag.lzw.service.TaskTrackHandel;
import com.ag.lzw.vo.FeedBackVo;
import com.ag.lzw.vo.NewTaskVo;
import com.ag.lzw.vo.TaskAnnexVo;
import com.ag.lzw.vo.TaskDetail;
import com.ag.lzw.vo.TaskOverVo;
import com.ag.lzw.vo.TrackVo;

@Service("handelTaskImpl")
public class HandelTasksImpl implements HandelTasks {

	@Resource(name = "lzwTaskDao")
	private LzwTaskDAO taskDAO;
	@Resource(name = "LzwTaskStatusDao")
	private TbTaskStatusDAO taskStatusDAO;
	@Resource(name = "lzwTbTaskFeedbackDAO")
	private LzwTbTaskFeedbackDAO taskFeedBackDAO;
	@Resource(name = "lzwTbOverRequestDAO")
	private LzwTbOverRequestDAO overResDAO;
	@Resource(name = "lzwTbEventDAO")
	private LzwTbEventDAO eventDAO;
	@Resource(name = "lzwTbTaskAnnexDAO")
	private LzwTbTaskAnnexDAO taskAnnexDAO;
	@Resource(name = "lzwTbFeedbackAnnexDAO")
	private LzwTbFeedbackAnnexDAO feedbackAnnexDAO;
	@Resource(name="LogService")
	private LogService logService;
	@Resource(name="lzwTbUserDAO")
	private LzwTbUserDAO userDAO;
    @Resource(name="TaskTrackHandelImpl")
	private TaskTrackHandel taskTrackHandel;

	private static final Logger logger = Logger
			.getLogger(HandelTasksImpl.class);

	/**
	 * 新任务保存
	 * 
	 * @param tasks
	 *            List<TaskInfo>
	 * @return void
	 */
	@Override
	public boolean saveTasks(List<NewTaskVo> tasks) {

		logger.info("Save Tasks!");
		TbTask newTask = new TbTask();
		logger.info("New Tasks Length:" + tasks.size());
		for (NewTaskVo ntv : tasks) {

			if (ntv != null) {
				logger.info(" ---Save Task ID:" + ntv.getId());
				newTask.setTId(ntv.getId());
				
				if(ntv.getFid()!=null){
					logger.info("Finding father task by fid:"+ntv.getFid());
					newTask.setTbTask(taskDAO.findATaskById(ntv.getFid())); // 设置父任务
					TbTask tt = this.taskDAO.findTaskDetailById(ntv.getFid());
					//检查上级任务的状态是否为“接受”
					if(!(tt.getTbTaskStatus().getTsId().equals("1001"))){
						logger.info("Update father task status--------");
						TbTaskStatus tts = new TbTaskStatus();
						tts.setTsId("1003");  //置上级任务状态为“执行”
						tt.setTbTaskStatus(tts);
						taskDAO.attachDirty(tt);
					}
				} else {
					newTask.setTbTask(null);
				}
				
				newTask.setTContext(ntv.getContext());
				newTask.setTName(ntv.getName());
				Date nowtime = new Date();
//				logger.info("Save time:"+DateHandeler.stringToDate(ntv.getTime()).toString());
				newTask.setTTime(nowtime);

				newTask.setB(ntv.getB()); // 把当前用户的信息保存在任务表的B字段，用于记录任务的分派者

				logger.info("find task event by id:"+ntv.getEId());
				TbEvent te = eventDAO.findAEventById(ntv.getEId());
				if (te != null) {
					newTask.setTbEvent(te); // 保存任务所属的事件ID
				} else {
					logger.info("Tasks id: " + ntv.getId()
							+ " save fail because of no vent loaded!");
					return false;
				}
				// 初始化新任务的状态
				newTask.setTbTaskStatus(this.taskStatusDAO.findById("1000"));
				logger.info("\t----TaskStatus:" + newTask.getTbTaskStatus().getTsName());
				// 保存用户ID
				TbUser tbuser = new TbUser();
				tbuser.setUId(ntv.getUserID());
				newTask.setTbUser(tbuser);
				// 保存部门ID
				TbUserDept tbdept = new TbUserDept();
				tbdept.setUdId(ntv.getDeptID());
				newTask.setTbUserDept(tbdept);
				newTask.setTTime(new Date());

				taskDAO.save(newTask);
				//任务附件的后续处理
				if(ntv.getFilename()!=null && ntv.getFilename().length()>0 && ntv.getFilename()!=null && ntv.getFilename().length()>0){
					String[] fileIdArr = ntv.getFileid().split(",");
					String[] fileNameArr = ntv.getFilename().split(",");
					// 批量处理任务附件的更新
					for (int i = 0; i < fileIdArr.length; i++) {
						String taskAnnexId = fileIdArr[i].substring(0, fileIdArr[i]
								.indexOf("."));// 过滤ID字符串的.doc
						//TbTaskAnnex ta = taskAnnexDAO.findById(taskAnnexId);
						TbTaskAnnex ta = new TbTaskAnnex();
						//ta.setTaId(new RandomStr().getRandomString(40));
						ta.setTaId(UUID.randomUUID().toString());
						ta.setB(fileIdArr[i]); // 用B字段记录附件在服务器端的文件名by hmk（内容如：66a...cda72.doc）
						ta.setTaName(fileNameArr[i]);
						TbTask tt = new TbTask();
						tt.setTId(ntv.getId());
						ta.setTbTask(tt);
						taskAnnexDAO.attachDirty(ta); // 更新附件表的信息
					}
				}
				
				//分派任务后，记录日志
				logService.saveByUser(ntv.getName(), ntv.getEId(), logService.LOG_TASK_DISPATCH, ntv.getB());
				logger.info("Svae Task Success!");
			} else {
				logger.info("this no task!");
			}
		}
		logger.info("Tasks Save Complete");
		return true;
	}

	/**
	 * 查询当前用户的任务
	 * 
	 * @param uid
	 * @return List<TaskDetail>
	 */
	@Override
	public List<TaskDetail> queryTaskofUser(String uid) {
		logger.info("Finding user's tasks");
		List<TbTask> tasks = this.taskDAO.findTasksofUser(uid);
		logger.info("tasks length = " + tasks.size());
		if (tasks.size() > 0) {
			List<TaskDetail> tasklist = new ArrayList<TaskDetail>();
			for (TbTask tt : tasks) {
				TaskDetail td = new TaskDetail(); // 用VO重新封装任务的信息
				logger.info("got task id:" + tt.getTId());
				td.setId(tt.getTId());
				td.setEId(tt.getTbEvent().getEId());
				td.setName(tt.getTName());
				td.setTime(DateHandeler.DateToString(tt.getTTime()));
				td.setStatus(tt.getTbTaskStatus().getTsName());
				td.setDept(tt.getTbUserDept().getUdName());
				td.setPhone(tt.getTbUser().getUPhone());
				tasklist.add(td);
			}
			return tasklist;
		} else {
			logger.info("Can not find any tasks of uid:" + uid);
			return null;
		}
	}

	/**
	 * 分页查询任务
	 * 
	 * @param sql
	 *            String
	 * @param firstRow
	 *            int
	 * @param maxRow
	 *            int
	 * @return List
	 */
	@Override
	public PageUtil queryTaskByPage(Map<String, String> queryOption, String uid,
			boolean isLeader) {

		int firstRow = 0;
		int maxRow = 30;
		int page = 1;
		int total = 0;
		if(queryOption.containsKey("page")){
			page = Integer.parseInt(queryOption.get("page"));
		}
		List<TbTask> list = null;

		String hql = "From TbTask as tt " 
		    + "left join fetch tt.tbEvent "
			+ "left join fetch tt.tbUserDept "
			+ "left join fetch tt.tbUser "
			+ "left join fetch tt.tbTaskStatus ";
		
		if (isLeader) {
			logger.info(">>>It is leader hql:");
			hql += "where ";
			
			logger.info(">>>queryGaven:"+queryOption.get("queryGaven"));
			logger.info(">>>queryMine:"+queryOption.get("queryMine"));
			
			if(queryOption.containsKey("queryGaven") 
					&& queryOption.get("queryGaven").equals("false")
					&& queryOption.get("queryMine").equals("false")){
				hql += "tt.b='"+ uid + "' "; //这句查询语句是默认的查询条件
			}
			hql += queryHql(queryOption,uid); //根据查询条件，并接hql语句

			hql += "order by tt.TTime desc";  //按时间降序排序
		} else {
			hql += "where ";
			logger.info(">>>It is not leader hql:");
			logger.info(">>>queryGaven:"+queryOption.get("queryGaven"));
			logger.info(">>>queryMine:"+queryOption.get("queryMine"));
			if(queryOption.containsKey("queryMine") 
					&& queryOption.get("queryGaven").equals("false")
					&& queryOption.get("queryMine").equals("false")){
				hql += "tt.tbUser.UId='"+ uid + "' ";  //这句查询语句是默认的查询条件
			}
			hql += queryHql(queryOption,uid); //根据查询条件，并接hql语句

			hql += "and tt.tbTaskStatus.tsId != '1002' " 
				   +"and tt.tbTaskStatus.tsId != '1006' " //排除办结状态与回退状态的任务 
				   +"order by tt.TTime desc";  //按时间降序排序
		}

		logger.info("query task page is:" + page);
		firstRow = maxRow * (page - 1);
		logger.info("the select hql is:"+hql);
		list = (List<TbTask>) this.taskDAO.findByPage(hql, firstRow, maxRow); // 取出分页的任务清单
		int size = list.size();
		logger.info("query tasks result's size is:" + size);

		if (size > 0) {
			List<TaskDetail> tasklist = new ArrayList<TaskDetail>();
			for (TbTask tt : list) { // 遍历任务清单
				TaskDetail td = new TaskDetail(); // 用VO重新封装任务的信息
				logger.info("get task id:"+tt.getTId());
				td.setId(tt.getTId());
				td.setEId(tt.getTbEvent().getEId());
				td.setName(tt.getTName());
				logger.info("get task time:"+tt.getTTime().toString());
				td.setTime(DateHandeler.DateToString(tt.getTTime()));
				td.setStatus(tt.getTbTaskStatus().getTsName());
				td.setSid(tt.getTbTaskStatus().getTsId());
				td.setDept(tt.getTbUserDept().getUdName());
				td.setPhone(tt.getTbUser().getUPhone());
				td.setUname(tt.getTbUser().getUName());
				//搜索任务的分派者信息
				TbUser tt2 = userDAO.findUserById(tt.getB());
				if(tt2!=null){
					td.setLeadname(tt2.getUName());
					td.setLeadphone(tt2.getUPhone());
					Iterator<TbUserDept> it = tt2.getTbUserDepts().iterator();
					String deptSms = "";
					while(it.hasNext()){
						TbUserDept tud = it.next();
						deptSms += tud.getUdName()+",";
					}
					if(deptSms.length()>0){
						String s = deptSms.substring(0, deptSms.length()-1);
						td.setLeadDepts(s);
					} else {
						td.setLeadDepts("");
					}
					
				}
                //获得该任务的新反馈
				int num = taskTrackHandel.getNewFeedbackSummary(tt.getTId());
				td.setNewFeedBack(String.valueOf(num));
				logger.info("new feedback num:"+num);
				//获得该任务的新办结申请
				int num2 = overResDAO.findNewReqByaskId(tt.getTId()).size();
				td.setNewOverReq(String.valueOf(num2));
				logger.info("new overResquest num :"+num2);
				tasklist.add(td);
			}
			total = this.taskDAO.getCountofTask(hql); // 获取当前用户任务的总数
			logger.info("the number of Tasks Found is:" + total);
			PageUtil pageUitl = new PageUtil();
			pageUitl.setMaxRow(maxRow);
			pageUitl.setPage(page);
			pageUitl.setTotal(total);
			pageUitl.setList(tasklist);
			return pageUitl;
		} else {
			logger.info("No Tasks were Found By Page:" + page);
			return null;
		}
	}

	/**
	 * 并接查询的hql语句
	 * @param queryOption
	 * @param hql
	 * @return
	 */
	private String queryHql(Map<String, String> queryOption, String uid){
		String hql = "";
		if(queryOption.containsKey("queryMine") 
				&& queryOption.get("queryMine").equals("true")
				&& queryOption.get("queryGaven").equals("false")){
			hql += "tt.tbUser.UId='"+ uid + "' ";
			logger.info(">>>1");
		} 
		if(queryOption.containsKey("queryGaven")
				&& queryOption.get("queryGaven").equals("true")
				&& queryOption.get("queryMine").equals("false")){
			hql += "tt.b='"+ uid + "' ";
			logger.info(">>>2");
		} 
		if(queryOption.containsKey("querybyDept") && !(queryOption.get("querybyDept").equals("null"))){
			hql += "and tt.tbUserDept.udName='"+queryOption.get("querybyDept")+"' ";
			logger.info(">>>3");
		} 
		if(queryOption.containsKey("queryByEid") && !(queryOption.get("queryByEid").equals("null"))){
			hql += "and tt.tbEvent.EId='"+queryOption.get("queryByEid")+"' ";
			logger.info(">>>4");
		}
		return hql;
	}
	
	@Override
	public void queryNewTaskAtFirst(String tid){
		TbTask tt = this.taskDAO.findTaskDetailById(tid);
		logger.info("Task Status:"+tt.getTbTaskStatus().getTsId());
		if(tt.getTbTaskStatus().getTsId().equals("1000")){
			TbTaskStatus tts = new TbTaskStatus();
			tts.setTsId("1001");
			tt.setTbTaskStatus(tts);
			taskDAO.attachDirty(tt);
		}
	}
	
	@Override
	public void changeTaskStatus(String tid,String sid){
		TbTask tt = this.taskDAO.findTaskDetailById(tid);
		TbTaskStatus tts = new TbTaskStatus();
		 if(!(tt.getTbTaskStatus().getTsId().equals(sid))){
			 tts.setTsId(sid);
			 tt.setTbTaskStatus(tts);
			 taskDAO.attachDirty(tt); 
		 } 
	}
	
	/**
	 * 根据任务ID查询其详情
	 * 
	 * @param tid
	 * @return TaskDetail
	 */
	@Override
	public TaskDetail queryTaskById(String tid) {
		logger.info("Finding task by id:" + tid);
		TbTask tt = this.taskDAO.findTaskDetailById(tid);
		
		if (tt != null) {
			
			TaskDetail td = new TaskDetail(); // 用VO重新封装任务的信息
			logger.info("got task id:" + tt.getTId());
			td.setId(tt.getTId());
			if (tt.getTbTask() != null) {
				td.setFid(tt.getTbTask().getTId()); // 寻找父任务id
			}
			
			td.setEId(tt.getTbEvent().getEId());
			td.setName(tt.getTName());
			td.setContext(tt.getTContext());
			td.setTime(DateHandeler.DateToString(tt.getTTime()));
			if(tt.getTbTaskStatus()!=null){
			logger.info("Task Status:"+tt.getTbTaskStatus());
			td.setStatus(tt.getTbTaskStatus().getTsName());
			}
			td.setDept(tt.getTbUserDept().getUdName());
			td.setPhone(tt.getTbUser().getUPhone());
			td.setUname(tt.getTbUser().getUName());
			
			//搜索任务的分派者信息
			TbUser tt2 = userDAO.findUserById(tt.getB());
			if(tt2!=null){
				td.setLeadname(tt2.getUName());
				td.setLeadphone(tt2.getUPhone());
				Iterator<TbUserDept> it = tt2.getTbUserDepts().iterator();
				String deptSms = "";
				while(it.hasNext()){
					TbUserDept tud = it.next();
					deptSms += tud.getUdName()+",";
				}
				if(deptSms.length()>0){
					String s = deptSms.substring(0, deptSms.length()-1);
					td.setLeadDepts(s);
				} else {
					td.setLeadDepts("");
				}
			}
			
			List<TbTaskAnnex> ttas = this.taskAnnexDAO.findByTaskId(td.getId()); // 获取任务附件信息
			List<TaskAnnexVo> annexs = new ArrayList<TaskAnnexVo>();

			for (TbTaskAnnex tta : ttas) { // 遍历附件结果
				TaskAnnexVo tav = new TaskAnnexVo();
				tav.setFilename(tta.getTaName());
				//tav.setFileid(tta.getTaId());
				tav.setFileid(tta.getB()); // 用B字段记录附件在服务器端的文件名by hmk（内容如：66a...cda72.doc）
				tav.setModuleId(AnnexPath.TASK_ANNEX_PATH);
				annexs.add(tav);
			}
			td.setAnnex(annexs);
			
			return td;
		} else {
			logger.info("Can not find any tasks of uid:" + tid);
			return null;
		}
	}


	/**
	 * 分页查询任务办结
	 */
	@Override
	public PageUtil getOverRequestByPage(Map<String, String> queryOption) {
		int firstRow = 0;
		int maxRow = 10;
		int page = 0;
		int total = 0;
		String tid = null;
		List<TbOverRequest> list = null;
		if (queryOption.containsKey("tid") && !queryOption.get("tid").isEmpty()) {
			tid = queryOption.get("tid");
			logger.info("===========getOverRequestByPage by tid:" + tid);
		} else {
			logger.info("======No id in queryOption!");
			return null;
		}
		String hql = "from TbOverRequest tor left join fetch tor.tbOverStatus left join fetch tor.tbTask tt left join fetch "
				+ " tt.tbTaskStatus  where tt.TId ='" + tid + "'";

		if (queryOption.containsKey("page")
				&& !queryOption.get("page").isEmpty()) {
			page = Integer.parseInt(queryOption.get("page"));
			logger.info("=====query track list at page:" + page);
			firstRow = maxRow * (page - 1);
			list = this.taskDAO.findByPage(hql, firstRow, maxRow);

		} else {
			logger.info("=========No Page in queryOption!");
			return null;
		}
		if (list.size() == 0) {
			logger.info("============No Track found! -- length:" + list.size());
			return null;
		} else {
			total = list.size();
			PageUtil pageUitl = new PageUtil();
			pageUitl.setMaxRow(maxRow);
			pageUitl.setPage(page);
			pageUitl.setTotal(total);
			pageUitl.setList(list);
			return pageUitl;

		}
	}

	
	/**
	 * 任务办结申请的方法
	 * 
	 */
	@Override
	public boolean saveOverRequest(TaskOverVo over) {
		TbOverRequest overres = new TbOverRequest();
		Date nowTime = (Date) new java.util.Date();
		overres.setToId(over.getToid());
		overres.setToReason(over.getToReason());
		overres.setToTime(nowTime);
//		overres.setB("申请办结"); // 用办结申请表的B字段来存储“申请办结”、“办结”和“退办”这三个状态
		overres.setC(over.getApplyPerson());       //保存办结申请人
		
		TbOverStatus tost = new TbOverStatus();
		tost.setSid("1000");
		overres.setTbOverStatus(tost);  //修改任务状态		
		
		TbTask t = this.taskDAO.findATaskById(over.getTaskid());
		if (t != null) {
			overres.setTbTask(t);
		} else {
			return false;
		}
		System.out.println("==========="+overres.getToTime());
		this.overResDAO.save(overres);
		
		logger.info("\t Save OverRequest Success!");
		return true;
	}

	/**
	 * 任务退办的方法
	 * 
	 */
	@Override
	public boolean saveOverFail(TaskOverVo over) {
		TbOverRequest overres = new TbOverRequest();
		Date nowTime = (Date) new java.util.Date();
		overres.setToId(over.getToid());
		overres.setToReason(over.getToReason());
		overres.setToTime(nowTime);
		overres.setC(over.getApplyPerson());       //保存办结申请人在c字段
		overres.setB(over.gettPerson());        //把退办人保存在B字段
//		overres.setB("退办"); // 用办结申请表的B字段来存储“申请办结”、“办结”和“退办”这三个状态
		
		TbOverStatus tost = new TbOverStatus();
		tost.setSid("1003");
		overres.setTbOverStatus(tost);  //修改任务状态		
		
		if (over.getToFailue() != null) { // 退办理由
			overres.setToFailue(over.getToFailue());
		}
		TbTask t = this.taskDAO.findATaskById(over.getTaskid());
		if (t != null) {
			overres.setTbTask(t);
		} else {
			logger.info("Save OverRequest faile!Because of No Task loaded!");
			return false;
		}
		this.overResDAO.attachDirty(overres);
		logger.info("\t Save OverRequest Success!");
		return true;
	}
	
	/**
	 * 任务办结的方法
	 * 
	 */
	@Override
	public boolean saveOverTask(TaskOverVo over) {
		TbOverRequest overres = new TbOverRequest();
		Date nowTime = (Date) new java.util.Date();
		overres.setToId(over.getToid());
		overres.setToReason(over.getToReason());
		overres.setToTime(nowTime);
		overres.setC(over.getApplyPerson());       //保存办结申请人在c字段
		overres.setB(over.gettPerson());        //把办结人保存在B字段
//		overres.setB("办结"); // 用办结申请表的B字段来存储“申请办结”、“办结”和“退办”这三个状态
		
		TbOverStatus tost = new TbOverStatus();
		tost.setSid("1002");
		overres.setTbOverStatus(tost);  //修改任务状态		
		
		TbTask t = this.taskDAO.findATaskById(over.getTaskid());
		if (t != null) {
			
//			TbTaskStatus ntts = this.taskStatusDAO.findById("1003"); // 同时修改任务详情表里的任务状态
//			t.setTbTaskStatus(ntts);
			t.setTHandeled(over.getThandeled()); // 增加处置建议
			this.taskDAO.attachDirty(t);
			overres.setTbTask(this.taskDAO.findATaskById(over.getTaskid()));
		} else {
			return false;
		}
		this.overResDAO.attachDirty(overres);
		logger.info("\t Save OverTask Success!");
		return true;
	}
	
	/**
	 * 指挥长任务办结的方法
	 * 
	 */
	@Override
	public boolean directSaveOverTask(TaskOverVo over) {
		TbOverRequest overres = new TbOverRequest();
		Date nowTime = (Date) new java.util.Date();
		overres.setToId(over.getToid());
		overres.setToReason("null");
		overres.setToTime(nowTime);
//		overres.setC(over.getApplyPerson());       //保存办结申请人在c字段
		overres.setB(over.gettPerson());        //把办结人保存在B字段
//		overres.setB("办结"); // 用办结申请表的B字段来存储“申请办结”、“办结”和“退办”这三个状态
		
		TbOverStatus tost = new TbOverStatus();
		tost.setSid("1002");
		overres.setTbOverStatus(tost);  //修改任务状态		
		
		TbTask t = this.taskDAO.findATaskById(over.getTaskid());
		if (t != null) {
			
//			TbTaskStatus ntts = this.taskStatusDAO.findById("1003"); // 同时修改任务详情表里的任务状态
//			t.setTbTaskStatus(ntts);
			t.setTHandeled(over.getThandeled()); // 增加处置建议
			this.taskDAO.attachDirty(t);
			overres.setTbTask(this.taskDAO.findATaskById(over.getTaskid()));
		} else {
			return false;
		}
		this.overResDAO.save(overres);
		logger.info("\t Save OverTask Success!");
		return true;
	}
	
	
	/**
	 * 收回办结申请的方法
	 * 
	 */
	public boolean getBackTaskOverRequest(TaskOverVo over) {
		TbOverRequest overres = new TbOverRequest();
		overres.setToId(over.getToid());
	    
		overres.setToReason(over.getToReason());
		overres.setToTime(this.overResDAO.findById(over.getToid()).getToTime());
		
		overres.setC(over.getApplyPerson());       //保存办结申请人
		
		TbOverStatus tost = new TbOverStatus();
		tost.setSid("1001");
		overres.setTbOverStatus(tost);  //修改任务状态
		
		TbTask t = this.taskDAO.findATaskById(over.getTaskid());
		if (t != null) {
			overres.setTbTask(t);
		} else {
			return false;
		}
		
		this.overResDAO.attachDirty(overres);
		logger.info("\t attachDirty OverTask Success!");
		return true;
	}

	public List<TbOverRequest> findTboverRequestByToId(String toid) {
		int firstRow = 0;
		int maxRow = 10;
		List<TbOverRequest> list = null;
		String hql = "from TbOverRequest tor left join fetch tor.tbOverStatus left join fetch tor.tbTask tt left join fetch "
				+ " tt.tbTaskStatus  where tor.toId ='" + toid + "'";
		list = this.taskDAO.findByPage(hql, firstRow, maxRow);
		return list;
	}

	public PageUtil getTrackListByPage(Map<String, String> q) {
		// TODO Auto-generated method stub
		return null;
	}
}
